package com.mjing.utils;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.UUID;
import java.util.regex.Pattern;

/**
 * @author wangsm 2020-05-29
 * @Description
 */
public class StringUtils extends org.apache.commons.lang3.StringUtils {

    /**
     * @return: java.lang.String
     * @Author: wangsm 2020-05-29
     * @Param: [content]
     * @Description: 剔出<html>的标签
     * 去除字符串中的空格,回车,换行符,制表符
     */
    public static String removeHtml(String content) {
        //剔出<html>的标签
        String txtcontent = content.replaceAll("</?[^>]+>", "");
        //去除字符串中的空格,回车,换行符,制表符
        txtcontent = txtcontent.replaceAll("<a>\\s*|\t|\r|\n</a>", "");
        return txtcontent;
    }

    /**
     * @return: java.lang.String
     * @Author: wangsm 2020-05-29
     * @Description: 获取uuid
     */
    public static String getUUID() {
        String uuid = UUID.randomUUID().toString();
        return uuid.replaceAll("-", "");
    }


    private static final byte SIZE = 62;

    /**
     * 获得字符串字节码对应的进制索引
     *
     * @param _i
     * @return
     */
    private static final byte decode(byte _i) {
        if (_i >= 48 && _i <= 57) {
            return (byte) (_i - 48);
        } else if (_i >= 65 && _i <= 90) {
            return (byte) (_i - 55);
        } else if (_i >= 97 && _i <= 122) {
            return (byte) (_i - 61);
        }
        return -1;
    }

    /**
     * 获得进制索引对应的字符字节码
     *
     * @param idx
     * @return
     */
    private static final byte encode(byte idx) {
        if (idx < 0 || idx > 61) {
            return -1;
        } else if (idx <= 9) {
            return (byte) (idx + 48);
        } else if (idx <= 35) {
            return (byte) (idx + 55);
        } else {
            return (byte) (idx + 61);
        }
    }

    /**
     * 返回code的下一个编码
     *
     * @param code 需要转换的码
     * @param size 计算其中的几位,也就是如果size的第一位发生了进位会被忽略
     * @return
     */
    public static final String nextCode(String code, int size) {
        if (code == null) {
            return null;
        }
        byte[] _s = code.getBytes();
        boolean up = true;
        int step = 0;
        for (int i = _s.length - 1; i >= 0; i--) {
            step++;
            byte idx = decode(_s[i]);
            byte nidx = (byte) ((idx + 1) % SIZE);
            _s[i] = encode(nidx);
            if (nidx != 0 || step >= size) {
                break;
            }
        }
        return new String(_s);
    }

    public static String encodeByMd5(String string) {
        try {
            // 确定计算方法
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            Base64.Encoder base64Encoder = Base64.getEncoder();
            // 加密字符串
            return base64Encoder.encodeToString(md5.digest(string.getBytes("utf-8")));
        } catch (UnsupportedEncodingException e) {

        } catch (NoSuchAlgorithmException e) {

        }
        return "";
    }


    public static void main(String[] args) {
        String a = "zzzzzzzzz";
        System.out.println(nextCode(a, 2));
    }

}
